package com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.controller;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.bean.XtPickUpPointPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.service.XtPickUpPointService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/basic_data_manage/xt_pick_up_point")
public class XtPickUpPointController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(XtPickUpPointController.class);

    @Autowired
    private XtPickUpPointService xtPickUpPointService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/basic_data_manage/xt_pick_up_point/xtpickuppoint";
    }


    /**
     * 根据提货点名称与cityId检索提货点
     *
     * @param key
     * @param cityId
     * @return
     */
    @RequestMapping("/queryPickUpPointByCondition")
    @ResponseBody
    public List<XtPickUpPointPojo> queryPickUpPointByCondition(String key, String cityId) {
        List<XtPickUpPointPojo> xtPickUpPointPojos = xtPickUpPointService.queryPickUpPointByCondition(key, cityId);
        return xtPickUpPointPojos;
    }


    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        return xtPickUpPointService.getPageList(param);
    }


    /**
     * 保存提货点对象
     *
     * @param xtPickUpPointJsonObj
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public Object save(@RequestBody(required = true) JSONObject xtPickUpPointJsonObj) throws Exception {
        if (xtPickUpPointJsonObj == null) {
            throwException("提货点保存参数错误!");
        }

        String opFlag = DataTypeUtil.toStringObject(xtPickUpPointJsonObj.get("opFlag"));
        if (opFlag == null) {
            throwException("提货点保操作类型异常参数错误!");
        }

        XtPickUpPointPojo xtPickUpPointPojo = xtPickUpPointService.convertToJavaBean(xtPickUpPointJsonObj, opFlag);

        int affectRows;

        if (opFlag.equals(SysConstant.OPER_ADD)) {
            affectRows = xtPickUpPointService.insert(xtPickUpPointPojo);
        } else {
            affectRows = xtPickUpPointService.update(xtPickUpPointPojo);
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("提货点保存成功!");

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("提货点保存失败!");
        }

        return baseResultMap;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestParam(required = true) String ids) throws Exception {
        if (DataTypeUtil.isEmptyStr(ids)) {
            throwException("提货点删除参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("提货点删除成功!");

        int affectRows = xtPickUpPointService.delete(ids);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("提货点删除失败!");
        }
        return baseResultMap;
    }

}
